// import shopcar from '../../shopcar';
import * as actionTypes from './constants';

const defaultstate = {
    homedata: [],
    shopcar: [],
    totalprice: 0,
    checkall: false
}

const reducer = (state = defaultstate, action) => {
    const { type, data } = action
    let temp = null;
    let index = null;
    let checkalls = null;
  
    switch (type) {
        case actionTypes.CHANGE_HOMEDATA: // 初始化home界面的数据
            // console.log("进入了reducer")
            return { ...state, homedata: data }


        case actionTypes.COUNT_ALL:
            let total = 0;
            console.log(data, "---------------");
            console.log(data.length);
            console.log("进入了COUNT_ALL");
            for (let i = 0; i < data.length; i++) {
                if (data[i].check != false) {
                    total += data[i].num * (data[i].prc * 1);
                }
            }
            console.log(total);

            return { ...state, totalprice: total }

        case actionTypes.CHANGE_SHOPCAR:   // 对购物车进行增加操作，并且判断是否为唯一id
            temp = state.shopcar;
            console.log(data, "--------");
            index = state.shopcar.findIndex(state => state.id == data.id);
            if (index != -1) { //找到了相同的index，需要进行处理

                temp[index].num = state.shopcar[index].num + data.num;
                console.log(temp[index].num, "--------------------------------")
                return { ...state, shopcar: [...temp] }
            } else { // 没有找到相同的index，直接 添加进入shopcar

                temp = [...state.shopcar, data];
                return { ...state, shopcar: [...temp] }
            }


        //   上一个版本，无法重复检测购物车id，无法根据id生成唯一的商品对象
        // temp = [...state.shopcar, data];
        // return { ...state, shopcar: [...temp] }

        case actionTypes.DECREMENT_NUM: // 对指定id的num进行减少操作
            temp = state.shopcar;
            // findIndex()方法返回数组中满足提供的测试函数的第一个元素的索引。若没有找到对应元素则返回-1。

            index = temp.findIndex(temp => temp.id == data);
            // console.log(index);
            temp[index].num = temp[index].num - 1
            return {
                ...state, shopcar: [
                    ...temp
                ]
            }

        case actionTypes.INCREMENT_NUM: // 对指定id的num进行增加操作
            temp = state.shopcar;
            index = temp.findIndex(temp => temp.id == data);
            temp[index].num = temp[index].num + 1
            return {
                ...state, shopcar: [
                    ...temp
                ]
            }

        case actionTypes.CHANGE_CHECK:
            temp = state.shopcar;
            index = temp.findIndex(temp => temp.id == data);
            temp[index].check = !temp[index].check;
            console.log(temp[index].check);

            return { ...state, shopcar: [...temp] }

        case actionTypes.CHANGE_ALL:
            // console.log(data.length);
            checkalls = !state.checkall;
            console.log(data[0]);
            for (let i = 0; i < data.length; i++) {
                data[i].check = checkalls;
            }
            // console.log(checkalls);
            return { ...state, shopcar: data, checkall: checkalls }
        default:
            return state;
    }
}
export default reducer;